home *** CD-ROM | disk | FTP | other *** search
/ HAKERIS 11 / HAKERIS 11.ISO / linux / system / LinuxConsole 0.4 / linuxconsole0.4install-en.iso / guile0.4.lcm / share / guile / 1.6.0 / srfi / srfi-16.scm < prev    next >
Encoding:
Text File  |  2004-01-06  |  5.3 KB  |  152 lines

  1. ;;; srfi-16.scm --- case-lambda
  2.  
  3. ;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
  4. ;;
  5. ;; This program is free software; you can redistribute it and/or
  6. ;; modify it under the terms of the GNU General Public License as
  7. ;; published by the Free Software Foundation; either version 2, or
  8. ;; (at your option) any later version.
  9. ;;
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ;; General Public License for more details.
  14. ;;
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this software; see the file COPYING.  If not, write to
  17. ;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. ;; Boston, MA 02111-1307 USA
  19. ;;
  20. ;; As a special exception, the Free Software Foundation gives permission
  21. ;; for additional uses of the text contained in its release of GUILE.
  22. ;;
  23. ;; The exception is that, if you link the GUILE library with other files
  24. ;; to produce an executable, this does not by itself cause the
  25. ;; resulting executable to be covered by the GNU General Public License.
  26. ;; Your use of that executable is in no way restricted on account of
  27. ;; linking the GUILE library code into it.
  28. ;;
  29. ;; This exception does not however invalidate any other reasons why
  30. ;; the executable file might be covered by the GNU General Public License.
  31. ;;
  32. ;; This exception applies only to the code released by the
  33. ;; Free Software Foundation under the name GUILE.  If you copy
  34. ;; code from other Free Software Foundation releases into a copy of
  35. ;; GUILE, as the General Public License permits, the exception does
  36. ;; not apply to the code that you add in this way.  To avoid misleading
  37. ;; anyone as to the status of such modified files, you must delete
  38. ;; this exception notice from them.
  39. ;;
  40. ;; If you write modifications of your own for GUILE, it is your choice
  41. ;; whether to permit this exception to apply to your modifications.
  42. ;; If you do not wish that, delete this exception notice.
  43.  
  44. ;;; Author: Martin Grabmueller
  45.  
  46. ;;; Commentary:
  47.  
  48. ;; Implementation of SRFI-16.  `case-lambda' is a syntactic form
  49. ;; which permits writing functions acting different according to the
  50. ;; number of arguments passed.
  51. ;;
  52. ;; The syntax of the `case-lambda' form is defined in the following
  53. ;; EBNF grammar.
  54. ;;
  55. ;; <case-lambda>
  56. ;;    --> (case-lambda <case-lambda-clause>)
  57. ;; <case-lambda-clause>
  58. ;;    --> (<signature> <definition-or-command>*)
  59. ;; <signature>
  60. ;;    --> (<identifier>*)
  61. ;;      | (<identifier>* . <identifier>)
  62. ;;      | <identifier>
  63. ;;
  64. ;; The value returned by a `case-lambda' form is a procedure which
  65. ;; matches the number of actual arguments against the signatures in
  66. ;; the various clauses, in order.  The first matching clause is
  67. ;; selected, the corresponding values from the actual parameter list
  68. ;; are bound to the variable names in the clauses and the body of the
  69. ;; clause is evaluated.
  70.  
  71. ;;; Code:
  72.  
  73. (define-module (srfi srfi-16)
  74.   :export-syntax (case-lambda))
  75.  
  76. (cond-expand-provide (current-module) '(srfi-16))
  77.  
  78. (define-macro (case-lambda . clauses)
  79.  
  80.   ;; Return the length of the list @var{l}, but allow dotted list.
  81.   ;;
  82.   (define (alength l)
  83.     (cond ((null? l) 0)
  84.       ((pair? l) (+ 1 (alength (cdr l))))
  85.       (else 0)))
  86.  
  87.   ;; Return @code{#t} if @var{l} is a dotted list, @code{#f} if it is
  88.   ;; a normal list.
  89.   ;;
  90.   (define (dotted? l)
  91.     (cond ((null? l) #f)
  92.       ((pair? l) (dotted? (cdr l)))
  93.       (else #t)))
  94.  
  95.   ;; Return the expression for accessing the @var{index}th element of
  96.   ;; the list called @var{args-name}.  If @var{tail?} is true, code
  97.   ;; for accessing the list-tail is generated, otherwise for accessing
  98.   ;; the list element itself.
  99.   ;;
  100.   (define (accessor args-name index tail?)
  101.     (if tail?
  102.     (case index
  103.       ((0) `,args-name)
  104.       ((1) `(cdr ,args-name))
  105.       ((2) `(cddr ,args-name))
  106.       ((3) `(cdddr ,args-name))
  107.       ((4) `(cddddr ,args-name))
  108.       (else `(list-tail ,args-name ,index)))
  109.     (case index
  110.       ((0) `(car ,args-name))
  111.       ((1) `(cadr ,args-name))
  112.       ((2) `(caddr ,args-name))
  113.       ((3) `(cadddr ,args-name))
  114.       (else `(list-ref ,args-name ,index)))))
  115.  
  116.   ;; Generate the binding lists of the variables of one case-lambda
  117.   ;; clause.  @var{vars} is the (possibly dotted) list of variables
  118.   ;; and @var{args-name} is the generated name used for the argument
  119.   ;; list.
  120.   ;;
  121.   (define (gen-temps vars args-name)
  122.     (let lp ((v vars) (i 0))
  123.       (cond ((null? v) '())
  124.         ((pair? v) 
  125.          (cons `(,(car v) ,(accessor args-name i #f))
  126.            (lp (cdr v) (+ i 1))))
  127.         (else `((,v ,(accessor args-name i #t)))))))
  128.  
  129.   ;; Generate the cond clauses for each of the clauses of case-lambda,
  130.   ;; including the parameter count check, binding of the parameters
  131.   ;; and the code of the corresponding body.
  132.   ;;
  133.   (define (gen-clauses l length-name args-name)
  134.     (cond ((null? l) (list '(else (error "too few arguments"))))
  135.       (else
  136.        (cons
  137.         `((,(if (dotted? (caar l)) '>= '=)
  138.            ,length-name ,(alength (caar l)))
  139.           (let ,(gen-temps (caar l) args-name)
  140.           ,@(cdar l)))
  141.         (gen-clauses (cdr l) length-name args-name)))))
  142.  
  143.   (let ((args-name (gensym))
  144.     (length-name (gensym)))
  145.     (let ((proc
  146.        `(lambda ,args-name
  147.           (let ((,length-name (length ,args-name)))
  148.         (cond ,@(gen-clauses clauses length-name args-name))))))
  149.       proc)))
  150.  
  151. ;;; srfi-16.scm ends here
  152.